package com.goldgov.column.web;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.goldgov.column.service.Column;
import com.goldgov.column.service.ColumnInit;
import com.goldgov.column.service.ColumnQuery;
import com.goldgov.column.service.ColumnService;
import com.goldgov.kcloud.core.json.JsonObject;
import com.goldgov.kcloud.core.json.JsonSuccessObject;
import com.goldgov.login.web.model.LoginAuthUser;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import springfox.documentation.annotations.ApiIgnore;

@RestController
@RequestMapping("/portal/column")
@Api("栏目")
public class ColumnController {

	@Autowired
	private ColumnService columnService;
	
	/**
	 * <党建头条>
	 * @author RongSL
	 * @version 1.0.0
	 * @param query
	 * @param request
	 * @param model
	 * @return
	 */
	@GetMapping("/getDjttByUser")
	@ApiOperation(value = "根据用户查询党建头条" )
	public JsonObject<Object> getDjttByUser(@ApiIgnore ColumnQuery query , HttpServletRequest request,HttpServletResponse response,Model model){
		LoginAuthUser user=LoginAuthUser.getUser(request);
		query.setSearchUserID(user.getEntityID());
		//查询个人经常访问栏目
		List<Column> uclist=columnService.getLikeByUser(query);
		List<String> idlist=new ArrayList<>();
		if(!uclist.isEmpty()) {
			//TODO 默认添加党建头条 为了解决爬取内容没有图片的问题而暂时添加
			idlist.add(ColumnQuery.COLUMN_DJTT.toString());
			//因为日志中存的是栏目中文名称，所以把栏目转换为ID
			exit:for(Column uc:uclist) {
				if(ColumnInit.getCategorytitleconvertid().containsKey(uc.getModuleName())) {
					idlist.add(ColumnInit.getCategorytitleconvertid().get(uc.getModuleName()));
					if(uclist.size()==3) {
						break exit;
					}
				}
			}
		}
		//删除党建头条
//		idlist.remove("154");
		JsonSuccessObject.SUCCESS.setData(idlist);
		return JsonSuccessObject.SUCCESS;
	}
	
	/**
	 * <统计栏目访问量>
	 * @author RongSL
	 * @version 1.0.0
	 * @param query
	 * @param request
	 * @param model
	 * @return
	 */
	@GetMapping("/statisticsFwl")
	@ApiOperation(value = "根据时间查询所有栏目访问量" )
	@ApiImplicitParams({ 
		@ApiImplicitParam(name = "searchStartDate", value = "开始时间", paramType = "query"),
		@ApiImplicitParam(name = "searchEndDate", value = "结束时间", paramType = "query")
	})
	public JsonObject<Object> statisticsFwl(@ApiIgnore ColumnQuery query , HttpServletRequest request,HttpServletResponse response,Model model){
		List<Column> uclist=columnService.statisticsFwl(query);
		List<Column> columnlist=new ArrayList<>();
		List<Column> colist=new ArrayList<>();
		if(!uclist.isEmpty()) {
			Map<String,String> map=new HashMap<>();
			//因为日志中存的是栏目中文名称，所以把栏目转换为ID
			for(Column uc:uclist) {
				if(ColumnInit.getCategorytitleconvertidall().containsKey(uc.getModuleName())) {
					columnlist.add(uc);
					map.put(uc.getModuleName(),"OK");
				}
			}
			//补齐所有栏目并去除已有数据
			for(int i=0;i<ColumnInit.getColumnlistall().size();i++) {
				if(!map.containsKey(ColumnInit.getColumnlistall().get(i).getModuleName())) {
					colist.add(ColumnInit.getColumnlistall().get(i));
				}
			}
			columnlist.sort((c1,c2)-> c1.getClickNum().compareTo(c2.getClickNum()));
		}else {
			columnlist.addAll(ColumnInit.getColumnlistall());
		}
		colist.addAll(columnlist);
		
		Collections.sort(colist, Comparator.comparing(Column::getClickNum).reversed());
		JsonSuccessObject.SUCCESS.setData(colist);
		return JsonSuccessObject.SUCCESS;
	}
}